﻿/*!	@file	r_gui_event.cpp
	@brief	GUIイベントクラス
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_gui_event.h"
#include "../input/r_input_server.h"
#include "../input/r_input_device_controll.h"
#include "../input/r_input_device_keys.h"
#include "../timer/r_timer_system.h"

namespace r{
	namespace gui{
		//! ダブルクリック間隔
		const r_ui32	s_rguievent_doubleclick_interval = 20;


		Event :: Event()
			:
			LMBDragWidget ( NULL ),
			LMBOldDragWidget ( NULL ),
			LMBDropWidget ( NULL ),
			LMBDoubleClick ( 0xffffffff ),
			LMBDoubleClicked ( false ),
			RMBDoubleClick ( 0xffffffff ),
			RMBDoubleClicked ( false ),
			KeyFocusWidget ( NULL )
		{
		}

		Event :: ~Event()
		{
		}

		void	Event :: update()
		{
			r::input::Server::instance		input;
			const r::input::DeviceControll*	controll	= input->getDeviceControll ( 0 );
			const r::input::DeviceKeys*		keys		= input->getDeviceKeys ( 0 );
			const	r::input::Screen&		scrpos		= controll->getScreen();
			curX										= scrpos.x;
			curY										= scrpos.y;
			r_ui32							allpush		= controll->getAllKeyPush();
			r_ui32							allpress	= controll->getAllKeyPress();
			r_ui32							allrelease	= controll->getAllKeyRelease();
			// ダブルクリックフラグをオフにする
			LMBDoubleClicked							= false;
			RMBDoubleClicked							= false;
			// 各種プレスとプッシュフラグを設定していく
			LMBPressed									= allpress & r::input::DeviceControllKeyMask_PointL ? true : false;
			LMBPushed									= allpush & r::input::DeviceControllKeyMask_PointL ? true : false;
			LMBReleased									= allrelease & r::input::DeviceControllKeyMask_PointL ? true : false;
			RMBPressed									= allpress & r::input::DeviceControllKeyMask_PointR ? true : false;
			RMBPushed									= allpush & r::input::DeviceControllKeyMask_PointR ? true : false;
			RMBReleased									= allrelease & r::input::DeviceControllKeyMask_PointR ? true : false;

			// 左クリックがリリースされたらdrag対象はもうない
			LMBOldDragWidget		= LMBDragWidget;
			if ( LMBReleased == true ) {
				LMBDragWidget		= NULL;
			}
			LMBDropWidget			= NULL;

			OldKeyFocusWidget		= KeyFocusWidget;

			r_ui32	*clickcount[2]						= {&LMBDoubleClick, &RMBDoubleClick};
			r_bool	*clicked[2]							= {&LMBPushed, &RMBPushed};
			r_bool	*dclicked[2]						= {&LMBDoubleClicked, &RMBDoubleClicked};
			// ダブルクリック判定
			r::timer::TimerSystem::instance		systimer;
			for ( r_ui32 i = 0; i < 2; ++i ) {
				if ( *clicked[i] ) {
					if ( *clickcount[i] < s_rguievent_doubleclick_interval ) {
						*dclicked[i] = true;
						*clickcount[i] = 0xffffffff;
					} else {
						*clickcount[i] = 0;
					}
				}
				if ( *clickcount[i] < s_rguievent_doubleclick_interval ) {
					( *clickcount[i] ) += systimer->getDeltaFrameFlex();
				}
			}
		}

		void	Event :: setLMBDrag ( Widget* widget )
		{
			dragBeginX		= curX;
			dragBeginY		= curY;
			LMBDragWidget	= widget;
		}

		void	Event :: setLMBDrop ( Widget* widget )
		{
			LMBDropWidget	= widget;
		}

		void	Event :: setKeyboardFocus ( Widget* widget )
		{
			KeyFocusWidget	= widget;
		}

		void	Event :: moveKeyboardFocus()
		{
			// 現在はダミーでキルフォーカス
			KeyFocusWidget	= NULL;
		}

		r_bool	Event :: getLMBHover() const
		{
			return ! ( LMBPressed | LMBPushed );
		}

		r_bool	Event :: getLMBClick() const
		{
			return LMBPushed;
		}

		r_bool	Event :: getLMBDClick() const
		{
			return LMBDoubleClicked;
		}

		r_bool	Event :: getLMBPress() const
		{
			return LMBPressed;
		}

		r_bool	Event :: getLMBRelease() const
		{
			return LMBReleased;
		}

		r_bool	Event :: getRMBHover() const
		{
			return ! ( RMBPressed | RMBPushed );
		}

		r_bool	Event :: getRMBClick() const
		{
			return RMBPushed;
		}

		r_bool	Event :: getRMBDClick() const
		{
			return RMBDoubleClicked;
		}

		r_bool	Event :: getRMBPress() const
		{
			return RMBPressed;
		}

		r_bool	Event :: getRMBRelease() const
		{
			return RMBReleased;
		}

		void	Event :: getCurPos ( r_si32& x, r_si32& y ) const
		{
			x	= curX;
			y	= curY;
		}

		void	Event :: getDragPos ( r_si32& x, r_si32& y ) const
		{
			x	= dragBeginX;
			y	= dragBeginY;
		}

		Widget*	Event :: getLMBDragWidget() const
		{
			return LMBDragWidget;
		}

		Widget*	Event :: getLMBOldDragWidget() const
		{
			return LMBOldDragWidget;
		}

		Widget*	Event :: getLMBDropWidget() const
		{
			return LMBDropWidget;
		}

		Widget*	Event :: getKeyboardFocusWidget() const
		{
			return KeyFocusWidget;
		}

		Widget*	Event :: getOldKeyboardFocusWidget() const
		{
			return OldKeyFocusWidget;
		}
	}
}